import { fetchPerson } from '../../../services/usercenter/fetchPerson';
import { phoneEncryption } from '../../../utils/util';
import Toast from 'tdesign-miniprogram/toast/index';
import useStore from '../../../store/index'
Page({
  data: {
    personInfo: {
      avatarUrl: '',
      nickName: '',
      gender: 0,
      phoneNumber: '',
    },
    showUnbindConfirm: false,
    pickerOptions: ['男', '女'],
    genderMap: ['', '男', '女'],
  },
  onSetUserInfo() {
    useStore.userStore.Set_userStore(this.data.personInfo)
  },
  openUnbindConfirm(res) {
  },
  onShow() {
    this.init();
  },
  init() {
    this.fetchData();
  },
  fetchData() {
    const { userStore } = useStore
    const personInfo = userStore.info
    this.setData({
      personInfo,
      'personInfo.phoneNumber': phoneEncryption(personInfo.phoneNumber),
    });
  },
  onClickCell({ currentTarget }) {
    const { dataset } = currentTarget;
    const { nickName } = this.data.personInfo;

    switch (dataset.type) {
      case 'name':
        wx.navigateTo({
          url: `/pages/usercenter/name-edit/index?name=${nickName}`,
        });
        break;
      case 'avatarUrl':
        this.toModifyAvatar();
        break;
      case 'phoneNumber':
        Toast({
          context: this,
          selector: '#t-toast',
          message: '你点击了手机号,嘿嘿',
          theme: 'info',
        });
        break;
      default: {
        break;
      }
    }
  },
  onConfirm(e) {
    const { value } = e.detail;
    this.setData(
      {
        'personInfo.gender': +value + 1,
      },
      () => {
        Toast({
          context: this,
          selector: '#t-toast',
          message: '设置成功',
          theme: 'success',
        });
      },
    );
    this.onSetUserInfo()
  },
  async toModifyAvatar() {
    try {
      const tempFilePath = await new Promise((resolve, reject) => {
        wx.chooseImage({
          count: 1,
          sizeType: ['compressed'],
          sourceType: ['album', 'camera'],
          success: (res) => {
            const { path, size } = res.tempFiles[0];
            if (size <= 10485760) {
              resolve(path);
            } else {
              reject({ errMsg: '图片大小超出限制，请重新上传' });
            }
          },
          fail: (err) => reject(err),
        });
      });
      const tempUrlArr = tempFilePath.split('/');
      const tempFileName = tempUrlArr[tempUrlArr.length - 1];
      Toast({
        context: this,
        selector: '#t-toast',
        message: `已选择图片-${tempFileName}`,
        theme: 'success',
      });
    } catch (error) {
      if (error.errMsg === 'chooseImage:fail cancel') return;
      Toast({
        context: this,
        selector: '#t-toast',
        message: error.errMsg || error.msg || '修改头像出错了',
        theme: 'error',
      });
    }
  },
});
